package cn.tedu.test.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * SpringSecurity 提供认证信息的接口
 */
@Component
public class UserDetailsServiceImpl implements UserDetailsService {


    @Autowired
    PasswordEncoder passwordEncoder;


    /**
     * username 是登录表单上的用户信息
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
        if ("Jerry".equals(username)){
            //创建一个用户信息, 临时测试使用
            UserDetails userDetails = User.builder().username("Jerry")
                    .password(passwordEncoder.encode("1234"))
                    .roles("ADMIN").build();
            return userDetails;
        }else if ("Tom".equals(username)){
            UserDetails userDetails = User.builder().username("Tom")
                    .password(passwordEncoder.encode("1234"))
                    .roles("USER").build();
            return userDetails;
        }
        throw new UsernameNotFoundException("没有用户信息");
    }
}
